
(function () {
    var core = ecui,
        util = core.util,
        ui = ecui.ui,
        dom = ecui.dom,
        copyId = null;

    Object.assign(
        NS.data,
        {
            finCostTypeTree: [],
            costTypes: [
                { 'id': '1', 'name': '飞机' },
                { 'id': '2', 'name': '火车' },
                { 'id': '3', 'name': '餐饮' }
            ]
        }
    );
    Object.assign(
        NS.ui,
        {
            SaveBookBtn: ecui.inherits(
                ui.Control,
                function (el, options) {
                    ui.Control.call(this, el, options);
                    this._bAgain = options.again;
                },
                {
                    onclick: function () {
                        this.request(
                            'data@FORM /erp-management/cost/save?editBookForm',
                            function () {
                                var code = ecui.esr.getData('data_CODE');
                                var data = ecui.esr.getData('data');
                                if (code === 0) {
                                    if (ecui.esr.getData('fromEdit') === 'true') {
                                        ecui.dispatchEvent(ecui.esr.findRoute(this).owner, 'change', { item: data, copyId: copyId });
                                    }
                                    ecui.tip('warn', '创建费用成功');
                                    if (this._bAgain) {
                                        var route = ecui.esr.getRoute('/finance/book.edit');
                                        delete route.CACHE;
                                        ecui.esr.reload();
                                    } else {
                                        history.go(-1);
                                    }
                                }
                            }.bind(this),
                            function (err) {
                                if (!err) {
                                    ecui.tip('warn', '请完善必填信息');
                                }
                                return false;
                            }
                        );
                    }
                },
                ui.Submit
            )
        }
    );
    ecui.esr.addRoute('edit', {
        model: function (context, callback) {
            var model = [];
            if (context.id) {
                model.push('detail@GET /erp-management/cost/detail/${id}?');
            }
            if (context.copyId) {
                model.push('detail@GET /erp-management/cost/detail/${copyId}?');
            }
            ecui.esr.request(model, callback, function () {
                return true;
            });
            return false;
        },
        weight: 3,
        frame: true,
        onbeforerequest: function (context) {
            copyId = context.copyId;
        },
        onbeforerender: function (context) {
            if (mao.info.invoiceInfo) {
                NS.data.invoiceInfo = mao.info.invoiceInfo;
                delete mao.info.invoiceInfo;
            } else {
                NS.data.invoiceInfo = null;
            }
            // 处理费用类型数据
            var finCostTypeTree = [];
            var tmp;
            mao.info.baseInfo.finCostType.forEach(function (item) {
                if (item.id) {
                    tmp.children.push(Object.assign({}, item));
                } else {
                    tmp = Object.assign({ children: [] }, item);
                    finCostTypeTree.push(tmp);
                }
            });
            NS.data.finCostTypeTree = finCostTypeTree;
        },
        onafterrender: function (context) {
        }
    });
}());

